package com.charge.service.impl;

import com.charge.dao.AccountMapping;
import com.charge.dao.ChargeRecordMapping;
import com.charge.dao.UserIncomeTypeMapping;
import com.charge.dao.UserSpendingTypeMapping;
import com.charge.domain.Account;
import com.charge.domain.ChargeRecord;
import com.charge.dto.ChargeAddDTO;
import com.charge.dto.ChargeImportDTO;
import com.charge.dto.ChargeRecordDTO;
import com.charge.service.AccountService;
import com.charge.service.ChargeRecordService;
import com.charge.util.DateUtils;
import com.charge.vo.ChargeRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/8 16:23
 **/
@Service
public class ChargeRecordServiceImpl implements ChargeRecordService{

    @Autowired
    private ChargeRecordMapping chargeRecordMapping;

    @Autowired
    private AccountMapping accountMapping;

    @Autowired
    private UserSpendingTypeMapping spendingTypeMapping;

    @Autowired
    private UserIncomeTypeMapping incomeTypeMapping;

    @Override
    public Integer selectChargeRecordCount(ChargeRecordDTO chargeRecordDTO) {
        return chargeRecordMapping.selectChargeRecordCount(chargeRecordDTO);
    }

    @Override
    public List<ChargeRecordVO> selectChargeRecordList(ChargeRecordDTO chargeRecordDTO) {
        return chargeRecordMapping.selectChargeRecordList(chargeRecordDTO);
    }

    @Override
    @Transactional
    public void importChargeRecord(List<ChargeImportDTO> chargeImportDTOList, Integer userId) {
        for (ChargeImportDTO importDTO : chargeImportDTOList) {
            ChargeRecord chargeRecord = new ChargeRecord();

            chargeRecord.setUserId(userId);

            String accountName = importDTO.getAccountName();
            Account account = accountMapping.selectAccountByName(accountName);
            if (account == null) {
                throw new RuntimeException("账户不存在:" + accountName);
            }
            chargeRecord.setAccountType(account.getId());

            String paymentsName = importDTO.getPaymentsName();
            String projectName = importDTO.getProjectName();

            BigDecimal newBalance = null;

            if ("支出".equals(paymentsName)) {
                chargeRecord.setPaymentsType(1);
                Integer spendingId = spendingTypeMapping.selectIdByName(projectName);
                if (spendingId == null) {
                    throw new RuntimeException("不存在此支出类型:" + projectName);
                }
                chargeRecord.setSpendingType(spendingId);

                newBalance = account.getAccountBalance().add(importDTO.getMoney());
                account.setAccountBalance(newBalance);
                accountMapping.updateByPrimaryKeySelective(account);
            } else if ("收入".equals(paymentsName)) {
                chargeRecord.setPaymentsType(2);
                Integer incomeId = incomeTypeMapping.selectIdByName(projectName);
                if (incomeId == null) {
                    throw new RuntimeException("不存在此收入类型:" + projectName);
                }
                chargeRecord.setIncomeType(incomeId);

                newBalance = account.getAccountBalance().add(importDTO.getMoney());
                account.setAccountBalance(newBalance);
                accountMapping.updateByPrimaryKeySelective(account);
            }

            chargeRecord.setAccountBalance(newBalance);

            chargeRecord.setMoney(importDTO.getMoney());
            chargeRecord.setRemark(importDTO.getRemark());
            chargeRecord.setCreateTime(importDTO.getCreateDate());

            ChargeRecord repeatRecord = chargeRecordMapping.selectRepeatRecord(chargeRecord);
            if (repeatRecord != null) {
                throw new RuntimeException("可能会导入重复数据"
                        + " 收支类型:" + repeatRecord.getPaymentsType()
                        + " 时间:" + DateUtils.formatDate(repeatRecord.getCreateTime())
                        + " 备注:" + repeatRecord.getRemark());
            }
            chargeRecordMapping.insertSelective(chargeRecord);
        }
    }

    @Override
    @Transactional
    public void addChargeRecord(List<ChargeAddDTO> chargeAddDTOList, Integer userId) {
        try {
            for (ChargeAddDTO addDTO : chargeAddDTOList) {
                Account account = accountMapping.selectByPrimaryKey(addDTO.getAccountId());
                ChargeRecord record = new ChargeRecord();
                record.setAccountType(addDTO.getAccountId());
                record.setUserId(userId);
                Integer paymentsType = addDTO.getPaymentsType();
                record.setPaymentsType(addDTO.getPaymentsType());
                // 支出
                if (paymentsType == 1) {
                    record.setSpendingType(addDTO.getProjectId());
                } else if (paymentsType == 2) {
                    // 收入
                    record.setIncomeType(addDTO.getProjectId());
                }
                BigDecimal newBalance = account.getAccountBalance().add(addDTO.getMoney());
                record.setAccountBalance(newBalance);

                record.setMoney(addDTO.getMoney());
                record.setRemark(addDTO.getRemark());
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                record.setCreateTime(format.parse(addDTO.getCreateTime()));

                account.setAccountBalance(newBalance);
                accountMapping.updateByPrimaryKeySelective(account);

                chargeRecordMapping.insertSelective(record);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
